package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.admin.exception.BusinessException;
import com.tanhua.admin.mapper.AdminMapper;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.AuditStatus;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ManageService {

    @Autowired
    private AdminMapper adminMapper;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private MovementApi movementApi;

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private VideoApi videoApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    // 分页查询用户详情
    public PageResult findAllUsers(Integer page, Integer pagesize) {
        // 1:调用api分页查询数据列表 IPage<UserInfo>
        IPage iPage = userInfoApi.findAll(page, pagesize);
        // 2:需要将IPage转化为PageResult
        // 3:构造返回值
        return new PageResult(page,pagesize,iPage.getTotal(),iPage.getRecords());
    }


    // 根据id查询用户详情
    public ResponseEntity findById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        return ResponseEntity.ok(userInfo);
    }

    // 分页查询视频列表
    public PageResult findVideos(Long userId, Integer page, Integer pagesize) {
        // 1:调用api查询视频列表 (PageResult<video>)
        List<Video> list =videoApi.findByUserId(userId,page,pagesize);
        // 2:查询当前用户详情
        UserInfo info = userInfoApi.findById(userId);
        // 3:一个video对象转化为一个videoVo对象
        List<VideoVo> vos = new ArrayList<>();
        for (Video video : list) {
            VideoVo vo = VideoVo.init(info,video);
            vos.add(vo);
        }
        // 4:构造返回
        return new PageResult(page,pagesize,0L,vos);
    }


    // 查询指定用户发布的所有动态
    public PageResult findAllMovement(Integer page, Integer pagesize, Long uid, Integer state) {
        // 1:调用api查询当前用户发布的动态
        PageResult result = movementApi.findByUserId(uid, page, pagesize);
        List<Movement> list = (List<Movement>) result.getItems();
        // 2:查询当前用户详情
        UserInfo info = userInfoApi.findById(uid);
        // 3:一个数据构造一个vo对象
        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : list) {
            MovementsVo vo = MovementsVo.init(info,movement);
            vos.add(vo);
        }
        // 4:构建返回
        return new PageResult(page,pagesize,0L,vos);
    }


    // 根据动态id查询动态详情
    public MovementsVo findMovementById(String id) {
        // 1:根据动态id查询动态
        Movement movement = movementApi.findById(id);
        // 2:拿到动态发布者的id,查询用户详情
        if (movement == null){
            throw new BusinessException("wocao");
        }
        Long userId = movement.getUserId();
        UserInfo info = userInfoApi.findById(userId);
        // 3:构建vo对象
        MovementsVo vo = MovementsVo.init(info,movement);
        vo.setState(AuditStatus.WAITCHECK.getType());
        return vo;
    }


    // 分页查询评论列表
    public PageResult findComments(Integer page, Integer pagesize, String messageID) {
        // 1:根据动态id查询评论
        List<Comment> comments = commentApi.findComments(messageID, CommentType.COMMENT, page, pagesize);
        if(CollUtil.isEmpty(comments)){
            return new PageResult();
        }
        // 2:拿到评论人id
        List<Long> userIds = CollUtil.getFieldValues(comments, "userId", Long.class);
        // 3:根据评论人查询评论人信息

        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);
        // 4:构造对象返回
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = map.get(comment.getUserId());
            CommentVo vo = CommentVo.init(userInfo,comment);
            vos.add(vo);
        }
        return new PageResult(page,pagesize,Long.valueOf(comments.size()),vos);
    }
}
